<?php

namespace Xiaoc\Api;
/**
 * Xiaoc用户中心
 *
 * @author: zhy <zhy302813@163.com> 2017-8-19
 */

class User extends Common
{
    private $user_id;
    private $user_name;

    public function getRules()
    {
        $data["*"] = array(
            'user_token' => array('name' => 'user_token', 'require' => true, 'desc' => '用户token')
        );
        $data['edit_password'] = array(
            'old_password' => array('name' => 'old_password', 'desc' => '旧密码', 'require' => true),
            'new_password' => array('name' => 'new_password', 'desc' => '新密码', 'require' => true),
            'confirm_password' => array('name' => 'confirm_password', 'desc' => '确认密码', 'require' => true),
            'code' => array('name' => 'code', 'desc' => '验证码', 'require' => true, 'type' => 'int'),
        );
        $data['user_realname'] = array(
            'real_name' => array('name' => 'real_name', 'require' => true, 'desc' => '真实姓名'),
            'bank_mobile' => array('name' => 'bank_mobile', 'require' => true, 'desc' => '银行卡绑定手机号'),
            'self_num' => array('name' => 'self_num', 'require' => true, 'desc' => '身份证号'),
            'code' => array('name' => 'code', 'require' => true, 'desc' => '手机验证码'),
            'bank_card' => array('name' => 'bank_card', 'require' => true, 'desc' => '银行卡号')
        );
        $data['user_realname_edit'] = array(
            'real_name' => array('name' => 'real_name', 'require' => true, 'desc' => '真实姓名'),
            'bank_mobile' => array('name' => 'bank_mobile', 'require' => true, 'desc' => '银行卡绑定手机号'),
            'self_num' => array('name' => 'self_num', 'require' => true, 'desc' => '身份证号', 'type' => 'int'),
            'code' => array('name' => 'code', 'require' => true, 'desc' => '手机验证码', 'type' => 'int'),
            'bank_card' => array('name' => 'bank_card', 'require' => true, 'desc' => '银行卡号', 'type' => 'int')
        );
        $data['withdr_Log'] = array(
            'page' => array('name' => 'page', 'desc' => '页数', 'require' => true, 'default' => 1, 'type' => 'int'),
        );
        $data['withdr_Log_info'] = array(
            'withdr_id' => array('name' => 'withdr_id', 'desc' => '记录ID', 'require' => true, 'type' => 'int'),
        );
        $data['add_withdr'] = array(
            'pay_password' => array('name' => 'pay_password', 'desc' => '支付密码', 'require' => true),
            'amount' => array('name' => 'amount', 'desc' => '提现金额', 'require' => true, 'type' => 'float')
        );
        $data['set_pay_password'] = array(
            'password' => array('name' => 'password', 'desc' => '密码', 'require' => true),
            'confirm_password' => array('name' => 'confirm_password', 'desc' => '确认密码', 'require' => true),
            'code' => array('name' => 'code', 'desc' => '验证码', 'require' => true, 'type' => 'int'),
        );
        $data['seller_step_one'] = array(
            'agree' => array('name' => 'agree', 'desc' => '同意协议传1', 'require' => true, 'type' => 'int')
        );
        $data['seller_step_two'] = array(
            'sex' => array('name' => 'sex', 'desc' => '性别', 'require' => true),
            'name' => array('name' => 'name', 'desc' => '真实姓名', 'require' => true),
            'moblie' => array('name' => 'moblie', 'desc' => '手机号', 'require' => true),
            'eamil' => array('name' => 'eamil', 'desc' => '邮箱', 'require' => true),
        );
        $data['seller_step_three'] = array(
            'shop_type' => array('name' => 'shop_type', 'desc' => '店铺类型', 'require' => true),
            'one_category' => array('name' => 'one_category', 'desc' => '一级分类', 'require' => true),
            'two_category' => array('name' => 'two_category', 'desc' => '二级分类', 'require' => true),
        );
        $data['seller_step_four'] = array(
            'shop_suffix' => array('name' => 'shop_suffix', 'desc' => '店铺后缀', 'require' => true),
            'shop_name' => array('name' => 'shop_name', 'desc' => '店铺名称', 'require' => true),
            'login_name' => array('name' => 'login_name', 'desc' => '登录名', 'require' => true),
        );
        $data['add_address'] = array(
            'name' => array('name' => 'name', 'desc' => '姓名', 'require' => true),
            'province' => array('name' => 'province', 'desc' => '省ID', 'require' => true, 'type' => 'int'),
            'city' => array('name' => 'city', 'desc' => '市ID', 'require' => true, 'type' => 'int'),
            'area' => array('name' => 'area', 'desc' => '区ID', 'require' => true, 'type' => 'int'),
            'address' => array('name' => 'address', 'desc' => '详情地址', 'require' => true),
            'mobile' => array('name' => 'mobile', 'desc' => '手机号', 'require' => true),
            'is_default' => array('name' => 'is_default', 'desc' => '是否默认', 'require' => true, 'default' => 0)
        );
        $data['edit_address'] = array(
            'address_id' => array('name' => 'address_id', 'desc' => '地址ID', 'require' => true, 'type' => 'int'),
            'name' => array('name' => 'name', 'desc' => '姓名', 'require' => true),
            'province' => array('name' => 'province', 'desc' => '省ID', 'require' => true, 'type' => 'int'),
            'city' => array('name' => 'city', 'desc' => '市ID', 'require' => true, 'type' => 'int'),
            'area' => array('name' => 'area', 'desc' => '区ID', 'require' => true, 'type' => 'int'),
            'address' => array('name' => 'address', 'desc' => '详情地址', 'require' => true),
            'mobile' => array('name' => 'mobile', 'desc' => '手机号', 'require' => true),
            'is_default' => array('name' => 'is_default', 'desc' => '是否默认', 'require' => true, 'default' => 0)
        );
        $data['del_address'] = array(
            'address_id' => array('name' => 'address_id', 'desc' => '地址ID', 'require' => true, 'type' => 'int'),
        );
        $data['user_goods_collection'] = array(
            'page' => array('name' => 'page', 'desc' => '页数', 'require' => true, 'default' => 1, 'type' => 'int'),
        );
        $data['user_collection_del'] = array(
            'rec_id' => array('name' => 'rec_id', 'desc' => '多个id以逗号形式隔开')
        );
        $data['user_store_collection'] = array(
            'page' => array('name' => 'page', 'desc' => '页数', 'require' => true, 'default' => 1, 'type' => 'int'),
        );
        $data['user_upload_head'] = array(
            'file' => array(
                'name' => 'file',        // 客户端上传的文件字段
                'type' => 'file',
                'require' => true,
                'max' => 2097152,        // 最大允许上传2M = 2 * 1024 * 1024,
                'range' => array('image/jpeg', 'image/png'),  // 允许的文件格式
                'ext' => 'jpeg,jpg,png', // 允许的文件扩展名
                'desc' => '待上传的图片文件',
            ),
        );
        $data['get_address_info'] = array(
            'address_id' => array('name' => 'address_id', 'type' => 'int', 'desc' => '地址id')
        );
        $data['user_foot_del'] = array(
            'goods_id' => array('name' => 'goods_id', 'desc' => '多个id以逗号形式隔开')
        );
        $data['user_try_goods'] = array(
            'status' => array('name' => 'status', 'desc' => '0审核中1审核通过2未通过'),
        );
        $data['user_crowd_orders'] = array(
            'status' => array('name' => 'status', 'desc' => '1进行中2成功3成功'),
        );
        return $data;
    }

    /**
     * 请忽略
     * @desc 该接口已作废
     */
    protected function userCheck()
    {
        $user_ifno = $this->check_login($this->user_token);
        $this->user_id = $user_ifno['user_id'];
        $this->user_name = $user_ifno['user_name'];
    }

    /**
     * 用户中心
     * @desc 用户中心首页
     * @return int mobile_phone 手机号码
     * @return string nick_name 别名
     * @return string user_picture 头像
     * @return string rank_name 等级名称
     * @return string rank_points 等级积分
     * @return int pay_count 待付款
     * @return int stay_count 待发货
     * @return int confirmed_count 待收货
     * @return int reday_success 已完成
     * @return int retrun_count 退换货
     * @return float user_money 账户余额
     * @return float frozen_money 冻结资金
     * @return int  user_coupons 优惠券数量
     * @return int pay_points 积分
     * @return int level_1 一级会员
     * @return int level_2 二级会员
     * @return int level_3 三级会员
     *         int shop_step 是否已经商家入驻 为-1时不可再点击 反之是上次进行到第几步
     */
    public function index()
    {
        $user_id = $this->user_id;
        //获取用户信息
        $where['user_id'] = $this->user_id;
        $fieds = array('mobile_phone', 'nick_name', 'user_picture', 'user_rank');
        $data['user_info'] = $this->medoo->get('users', $fieds, $where);
        $data['user_info']['mobile_phone2'] = PassStart($data['user_info']['mobile_phone'], 3, 4);
        $data['user_info']['rank_name'] = rankNames($data['user_info']['user_rank']);

        //待付款的订单
        $alias = 'oi.';
        $where_pay = ' AND   ' . $alias . 'order_status ' . db_create_in(array(OS_CONFIRMED, OS_SPLITED)) . ' AND   ' . $alias . 'pay_status ' . db_create_in(array(PS_UNPAYED, PS_PAYED_PART)) . ' AND ( ' . $alias . 'shipping_status ' . db_create_in(array(SS_SHIPPED, SS_RECEIVED)) . ' OR ' . $alias . 'pay_id ' . db_create_in($this->payment_id_list(false)) . ') ';
        $data['my_order']['pay_count'] = $this->get_order_where_count($user_id, 0, $where_pay);
        //待发货
        $stay_pay = ' AND oi.order_status in (1,5,6) AND oi.shipping_status in (0,3,5) AND ( oi.pay_status in (1,2) OR oi.pay_id ' . db_create_in($this->payment_id_list()) . ')';
        $data['my_order']['stay_count'] = $this->get_order_where_count($user_id, 0, $stay_pay);
        //待收货
        $where_confirmed = ' AND oi.order_status in (1,5,6) AND oi.shipping_status = 1 AND oi.pay_status in (2,1) ';
        $data['my_order']['confirmed_count'] = $this->get_order_where_count($user_id, 0, $where_confirmed);
        //已完成
        $where_success = ' AND ' . $alias . 'order_status ' . db_create_in(array(OS_CONFIRMED, OS_SPLITED)) . ' AND ' . $alias . 'shipping_status ' . db_create_in(array(SS_RECEIVED)) . ' AND ' . $alias . 'pay_status ' . db_create_in(array(PS_PAYED, PS_PAYING)) . ' ';
        $data['my_order']['reday_success'] = $this->get_order_where_count($user_id, 0, $where_success);
        //退货退款的
        $return_where['user_id'] = $user_id;
        $data['my_order']['retrun_count'] = $this->medoo->count('order_return', $return_where);
        $user_money = $this->medoo->get('users', array('user_money', 'frozen_money', 'pay_points'), $where);
        $user_coupons = $this->medoo->count('coupons_user', array('user_id' => $user_id, 'is_use' => 0));
        $data['my_wallet']['user_money'] = $user_money['user_money'];
        $data['my_wallet']['frozen_money'] = $user_money['frozen_money'];
        $data['my_wallet']['pay_points'] = $user_money['pay_points'];
        $data['my_wallet']['user_coupons'] = (string)$user_coupons;
        //
        /*$shop_information = $this->medoo->get('merchants_shop_information', array('merchants_audit'), array('user_id' => $user_id));
        $shop = $this->medoo->get('merchants_steps_fields', array('steps_site'), $where);
        if ($shop['steps_site'] && $shop_information && $shop_information['merchants_audit'] != 1) {
            $data['shop_step'] = $shop['steps_site'] + 1;
        } elseif ($shop_information['merchants_audit'] == 1) {
            $data['shop_step'] = -1;
        } else {
            $data['shop_step'] = 1;
        }*/
        //我的团队
        $level_1 = $this->medoo->count('users', array('parent_id' => $user_id));
        $level_2 = \PhalApi\DI()->notorm->users->queryAll('SELECT  count(user_id) as c from dsc_users WHERE parent_id IN (SELECT user_id FROM dsc_users WHERE parent_id = ' . $user_id . ')', array());
        $level_3 = \PhalApi\DI()->notorm->users->queryAll('select count(user_id) as c FROM  dsc_users WHERE parent_id IN (Select user_id from dsc_users WHERE parent_id IN (SELECT user_id FROM dsc_users WHERE parent_id = ' . $user_id . '))', array());
        $data['my_team']['level_1'] = (string)$level_1;
        $data['my_team']['level_2'] = $level_2[0]['c'];
        $data['my_team']['level_3'] = $level_3[0]['c'];
        return $data;
    }

    /**
     * 会员修改密码
     * @desc    会员修改密码
     * @return array ret 200成功
     */
    public function edit_password()
    {
        $old_password = $this->old_password;
        $new_password = $this->new_password;
        $confirm_password = $this->confirm_password;
        if ($old_password && $new_password && $new_password == $confirm_password) {
            $where['user_id'] = $this->user_id;
            $fieds = array('password', 'ec_salt', 'mobile_phone');
            $res = $this->medoo->get('users', $fieds, $where);
            $res_code = check_code($res['mobile_phone'], $this->code);
            if (!$res_code) $this->api_error_msg('验证码错误', 1);
            $cf['password'] = $old_password;
            $cf['ec_salt'] = $res['ec_salt'];
            $password = $this->user_password($cf);
            if ($res && $res['password'] == $password) {
                $cf['password'] = $new_password;
                $data['password'] = $this->user_password($cf);
                $result = $this->medoo->update('users', $data, $where);
                if ($result) {
                    $this->api_success_msg('修改成功');
                } else {
                    $this->api_error_msg('修改失败');
                }
            } else {
                $this->api_error_msg('旧密码不正确');
            }
        } else {
            $this->api_error_msg('参数错误');
        }
    }

    /**
     * 用户资金明细
     * @desc 用户资金明细
     * @return string type 类型
     * @return string change_time 时间
     * @return float amount 产生的金额
     * @return string change_desc 说明
     */
    public function account_detail()
    {
        $where['user_id'] = $this->user_id;
        $where['LIMIT'] = array($this->limitStart, $this->limitEnd);
        $where['ORDER'] = array('log_id' => 'DESC');
        $where['user_money[!]'] = 0;
        $feid = array('user_money', 'change_time', 'change_desc');
        $list = $this->medoo->select('account_log', $feid, $where);
        $count = 0;
        if ($list) {
            foreach ($list as $key => $value) {
                $list[$key]['change_time'] = time_format($value['change_time']);
                $list[$key]['change_desc'] = msubstr($value['change_desc'], 0, 60);
                $value['user_money'] > 0 ? $list[$key]['type'] = '+' : $list[$key]['type'] = '';
                $list[$key]['amount'] = $value['user_money'];
            }
            $data['list'] = $list;
            $count_where['user_money[!]'] = 0;
            $count_where['user_id'] = $this->user_id;
            $count = $this->medoo->count('account_log', '*', $count_where);
        } else {
            $data['list'] = array();
        }
        $data['hasmore'] = $this->HasMore($count);
        return $data;
    }

    /**
     * 添加实名认证
     * @desc 添加实名认证
     * @return int ret 200成功
     */
    public function user_realname()
    {
        $real_name = $this->real_name;
        $bank_mobile = $this->bank_mobile;
        $self_num = $this->self_num;
        $code = $this->code;
        $bank_card = $this->bank_card;
        if (!isCreditNo($self_num, 1)) $this->api_error_msg('请填写正确的身份证号码');
        if (!check_code($bank_mobile, $code)) $this->api_error_msg('手机验证码错误');
        $bank_info = CheckBank($bank_card);
        if (!$bank_info) $this->api_error_msg('银行卡号不正确');
        $where['user_id'] = $this->user_id;
        $real_id = $this->medoo->get('users_real', array('real_id'), $where);
        if ($real_id) $this->api_error_msg('您已提交过实名认证');
        $add['real_name'] = trim($real_name);
        $add['bank_mobile'] = $bank_mobile;
        $add['self_num'] = $self_num;
        $add['bank_card'] = $bank_card;
        $add['bank_name'] = $bank_info['bank'] . '(' . $bank_info['nature'] . ')';
        $add['add_time'] = gmtime();
        $add['user_id'] = $this->user_id;
        $this->medoo->insert('users_real', $add);
        $insert_id = $this->medoo->id();
        if ($insert_id) {
            $this->api_success_msg('提交成功!请耐心等待管理员审核');
        } else {
            $this->api_error_msg('提交失败');
        }
    }

    /**
     * 实名认证详情
     * @desc 实名认证详情
     * @return string real_name 真实姓名
     * @return string bank_name 银行卡名称
     * @return int bank_card 银行卡号
     * @return int bank_mobile 银行绑定的手机号
     * @return int self_num 身份证号码
     * @return string add_time 添加时间
     * @return int review_status 审核状态0待审核1审核通过2审核未通过 当不为1时可以修改
     * @return string review_content审核备注
     */
    public function user_realname_info()
    {
        $where['user_id'] = $this->user_id;
        $field = array('real_name', 'bank_mobile', 'bank_name', 'bank_card', 'self_num', 'add_time', 'review_status', 'review_content');
        $real_info = $this->medoo->get('users_real', $field, $where);
        if ($real_info) {
            $real_info['add_time'] = time_format($real_info['add_time']);
            $info = $real_info;
        } else {
            $info = array();
        }
        return array('info' => $info);
    }

    /**
     * 编辑实名认证
     * @desc 编辑实名认证
     * @return int ret 200成功
     */
    public function user_realname_edit()
    {
        $real_name = $this->real_name;
        $bank_mobile = $this->bank_mobile;
        $self_num = $this->self_num;
        $code = $this->code;
        $bank_card = $this->bank_card;
        if (!isCreditNo($self_num, 1)) $this->api_error_msg('请填写正确的身份证号码');
        if (!check_code($bank_mobile, $code)) $this->api_error_msg('手机验证码错误');
        $bank_info = CheckBank($bank_card);
        if (!$bank_info) $this->api_error_msg('银行卡号不正确');
        $where['user_id'] = $this->user_id;
        $real_info = $this->medoo->get('users_real', array('real_id', 'review_status'), $where);
        if (!$real_info) $this->api_error_msg('抱歉!您还未提交实名认证');
        if ($real_info['review_status'] != 1) {
            $add['real_name'] = trim($real_name);
            $add['bank_mobile'] = $bank_mobile;
            $add['self_num'] = $self_num;
            $add['bank_card'] = $bank_card;
            $add['bank_name'] = $bank_info['bank'] . '(' . $bank_info['nature'] . ')';
            $add['review_status'] = 0;
            $res = $this->medoo->update('users_real', $add, $where);
            if ($res) {
                $this->api_success_msg('编辑成功!请耐心等待管理员审核');
            }
        } else {
            $this->api_error_msg('您已提交过实名认证');
        }
        $this->api_error_msg('编辑失败');
    }

    /**
     * 提现记录
     * @desc 提现记录
     * @return float amount 提现金额
     * @return string add_time 提现时间
     * @return int is_paid 提现状态 0 审核中 2已拒绝 3通过
     * @return int withdr_id 记录ID
     */
    public function withdr_Log()
    {
        $where['user_id'] = $this->user_id;
        $where['process_type'] = SURPLUS_RETURN;
        $where['LIMIT'] = array($this->limitStart, $this->limitEnd);
        $where['ORDER'] = array('add_time' => 'DESC');
        $field = array('amount', 'add_time', 'is_paid', 'id(withdr_id)');
        $list = $this->medoo->select('user_account', $field, $where);
        $count = 0;
        if ($list) {
            foreach ($list as $key => $value) {
                $list[$key]['add_time'] = time_format($value['add_time']);
            }
            $data['list'] = $list;
            unset($where['LIMIT']);
            $count = $this->medoo->count('user_account', '*', $where);
        } else {
            $data['list'] = array();
        }
        $data['hasmore'] = $this->HasMore($count);
        return $data;
    }

    /**
     * 提现记录详情
     * @desc 提现记录详情
     * @return float amount 提现金额
     * @return string add_time 提现时间
     * @return string admin_note 审核备注(这里要判断为真再显示)
     * @return int is_paid 提现状态 0 审核中 1通过 2已拒绝
     * @return string paid_time 审核时间(这里要判断为真再显示)
     * @return  string bank_number 卡号
     * @return  string real_name 真实姓名
     * @return  string user_note 会员备注
     */
    public function withdr_Log_info()
    {
        $withdr_id = $this->withdr_id;
        $join['[>]user_account_fields'] = array('user_account_fields.account_id' => 'id');
        $where['user_account.id'] = intval($withdr_id);
        $where['user_account.user_id'] = $this->user_id;
        $where['user_account.process_type'] = SURPLUS_RETURN;
        $field = array('user_account.amount', 'user_account.add_time', 'user_account.is_paid', 'user_account.paid_time', 'user_account.add_time', 'user_account.user_note', 'user_account_fields.bank_number', 'user_account_fields.real_name');
        $info = $this->medoo->get('user_account', $join, $field, $where);
        if ($info) {
            $info['bank_number'] = PassStart($info['bank_number'], 4, 4);
            $info['add_time'] = time_format($info['add_time']);
            $info['real_name'] = PassStart($info['real_name'], 1, 1);
            if ($info['paid_time']) $info['paid_time'] = time_format($info['paid_time']);
            return array('info' => $info);
        } else {
            return array('info' => array());
        }
    }

    /**
     * 会员提现
     * @desc 会员提现
     */
    public function add_withdr()
    {
        $user_id = $this->user_id;
        $amount = $this->amount;
        $pay_password = $this->pay_password;
        $where['user_id'] = $user_id;
        $password = $this->medoo->get('users_paypwd', array('pay_password', 'user_surplus', 'ec_salt'), $where);
        if ($password && $password['user_surplus'] == 1) {
            $cf['ec_salt'] = $password['ec_salt'];
            $cf['password'] = $pay_password;
            $res_password = $this->user_password($cf);
            if ($password['pay_password'] != $res_password) {
                $this->api_error_msg('支付密码不正确');
            }
        } else {
            $this->api_error_msg('您尚未设置支付密码');
        }
        $user_money = $this->medoo->get('users', array('user_money', 'frozen_money'), $where);
        if ($amount > $user_money['user_money']) {
            $this->api_error_msg('账户余额不足');
        }
        $field = array('real_name', 'bank_mobile', 'bank_name', 'bank_card', 'review_status');
        $real_info = $this->medoo->get('users_real', $field, $where);
        if ($real_info && $real_info['review_status'] == 1) {
            $data['user_id'] = $user_id;
            $data['admin_user'] = '';
            $data['amount'] = '-' . $amount;
            $data['add_time'] = gmtime();
            $data['paid_time'] = 0;
            $data['admin_note'] = '';
            $data['user_note'] = $real_info['bank_name'];
            $data['process_type'] = SURPLUS_RETURN;
            $data['payment'] = '';
            $data['is_paid'] = 0;
            $this->medoo->insert('user_account', $data);
            $account_id = $this->medoo->id();
            if ($account_id > 0) {
                $account_fields['user_id'] = $user_id;
                $account_fields['account_id'] = $account_id;
                $account_fields['bank_number'] = $real_info['bank_card'];
                $account_fields['real_name'] = $real_info['real_name'];
                $this->medoo->insert('user_account_fields', $account_fields);
                $fields_id = $this->medoo->id();
                if ($fields_id) {
                    //添加资金记录
                    $frozen_money = $amount;
                    $account_log = array('user_id' => $user_id, 'user_money' => $amount, 'frozen_money' => $frozen_money, 'rank_points' => 0, 'pay_points' => 0, 'change_time' => gmtime(), 'change_desc' => '【申请提现】 ' . $real_info['bank_name'], 'change_type' => ACT_ADJUSTING);
                    $this->medoo->insert('account_log', $account_log);
                    $log_id = $this->medoo->id();
                    //修改账户余额
                    if ($log_id) {
                        $up['user_money'] = $user_money['user_money'] - $amount;
                        $up['frozen_money'] = $user_money['frozen_money'] + $amount;
                        $res = $this->medoo->update('users', $up, $where);
                        if ($res) {
                            $this->api_success_msg('提现成功!请耐心等待管理员审核');
                        }
                    }
                }
            }
            $this->api_error_msg('提现失败');
        } else {
            $this->api_error_msg('您还未实名认证');
        }

    }

    /**
     * 设置支付密码
     * @desc 设置支付密码
     * @return ret 200 成功
     */
    public function set_pay_password()
    {
        $password = $this->password;
        $confirm_password = $this->confirm_password;
        $code = $this->code;
        $moblie = $this->get_user_mobile($this->user_id);
        if (!check_code($moblie, $code)) $this->api_error_msg('手机验证码错误');
        if ($password != $confirm_password) $this->api_error_msg('两次密码不一致');
        $where['user_id'] = $this->user_id;
        $user_password = $this->medoo->get('users', array('password', 'ec_salt'), $where);
        $login_password = $this->user_password(array('password' => $user_password['password'], 'ec_salt' => $user_password['ec_salt']));
        $info = $this->medoo->get('users_paypwd', array('pay_password', 'user_surplus', 'ec_salt'), $where);
        if ($info) {
            $cf['ec_salt'] = $info['ec_salt'];
            $cf['password'] = $password;
            $up['pay_password'] = $this->user_password($cf);
            if ($login_password == $up['pay_password']) $this->api_error_msg('不能设置为近期用过的密码');
            $res = $this->medoo->update('users_paypwd', $up, $where);
            if ($res) {
                $this->api_success_msg('修改成功');
            }
        } else {
            $data['pay_password'] = $this->user_password(array('password' => $password));
            if ($login_password == $data['pay_password']) $this->api_error_msg('不能设置为近期用过的密码');
            $data['user_surplus'] = 1;
            $data['user_id'] = $this->user_id;
            $data['pay_online'] = 1;
            $data['user_point'] = 1;
            $data['baitiao'] = 1;
            $data['gift_card'] = 1;
            $this->medoo->insert('users_paypwd', $data);
            $pay_id = $this->medoo->id();
            if ($pay_id) {
                $this->api_success_msg('设置成功');
            }
        }
        $this->api_error_msg('操作失败');
    }

    /**
     * 检测支付密码是否已设置
     * @desc 检测支付密码是否已设置
     * @return int status 1已设置0未设置
     */
    public function check_paypwd()
    {
        $where['user_id'] = $this->user_id;
        $info = $this->medoo->get('users_paypwd', 'user_id', $where);
        if ($info) {
            return array('status' => 1);
        } else {
            return array('status' => 0);
        }
    }

    /**
     * 商家入驻第一步
     * @desc 商家入驻第一步
     * @return ret 200 成功
     */
    public function seller_step_one()
    {
        $user_id = $this->user_id;
        $agree = $this->agree;
        $nformation_where['merchants_audit'] = 1;
        $nformation_where['user_id'] = $user_id;
        $shop_information = $this->medoo->get('merchants_shop_information', array('merchants_audit(status)'), $nformation_where);
        if ($shop_information) {
            $this->api_error_msg('您已经入驻了该平台');
        }
        $shop = $this->medoo->get('merchants_steps_fields', 'steps_site', array('user_id' => $user_id));
        if ($shop) $this->api_error_msg('您已经同意该平台的入驻协议');
        if (intval($agree) == 1) {
            $data['agreement'] = 1;
            $data['user_id'] = $user_id;
            $data['steps_site'] = 1;
            $this->medoo->insert('merchants_steps_fields', $data);
            $insert_id = $this->medoo->id();
            if ($insert_id) $this->api_success_msg('操作成功');
        }
        $this->api_error_msg('请同意该平台的入驻协议');
    }

    /**
     * 商家入驻第二步
     * @desc 商家入驻第二步
     * @return ret 200 成功
     */
    public function seller_step_two()
    {
        $user_id = $this->user_id;
        $data['contactXinbie'] = $this->sex;
        $data['contactName'] = $this->name;
        $data['contactPhone'] = $this->moblie;
        $data['contactEmail'] = $this->eamil;
        $data['steps_site'] = 2;
        $nformation_where['user_id'] = $user_id;
        $shop_information = $this->medoo->get('merchants_shop_information', array('merchants_audit(status)', 'shop_id'), $nformation_where);
        if ($shop_information['status'] == 1) {
            $this->api_error_msg('您已经入驻了该平台');
        }
        if (!isMobile($data['contactPhone'])) $this->api_error_msg('填写正确的手机号码');
        if (!isEmail($data['contactEmail'])) $this->api_error_msg('填写正确的邮箱');
        $where['user_id'] = $user_id;
        $shop = $this->medoo->get('merchants_steps_fields', 'fid', $where);
        if (empty($shop)) $this->api_error_msg('请同意该平台的入驻协议');
        $res = $this->medoo->update('merchants_steps_fields', $data, $where);
        if ($res) {
            if (empty($shop_information['shop_id'])) {
                $up_information['user_id'] = $user_id;
                $this->medoo->insert('merchants_shop_information', $up_information);
            }
            $this->api_success_msg('操作成功');
        }
        $this->api_error_msg('操作失败');
    }

    /**
     * 商家入驻第三步
     * @desc 商家入驻第三步
     * @return ret 200 成功
     */
    public function seller_step_three()
    {
        $user_id = $this->user_id;
        $data['shoprz_type'] = intval($this->shop_type);
        $data['shop_categoryMain'] = intval($this->one_category);
        $data['user_shopMain_category'] = trim($this->two_category, ',');
        $where['user_id'] = $user_id;
        $nformation_where['user_id'] = $user_id;
        $shop_information = $this->medoo->get('merchants_shop_information', array('merchants_audit(status)', 'shop_id'), $nformation_where);
        if ($shop_information['status'] == 1) {
            $this->api_error_msg('您已经入驻了该平台');
        }
        $shop = $this->medoo->get('merchants_steps_fields', 'contactName', $where);
        if (empty($shop)) $this->api_error_msg('请返回上一步填写信息');
        if (empty($data['shoprz_type']) || !in_array($data['shoprz_type'], array('2', '5', '6'))) $this->api_error_msg('请正确选择店铺类型');
        if ($shop_information) {
            $res = $this->medoo->update('merchants_shop_information', $data, $where);
            if ($res) {
                $data1['steps_site'] = 3;
                $this->medoo->update('merchants_steps_fields', $data1, $where);
                $this->api_success_msg('操作成功');
            }
        } else {
            $this->api_error_msg('请返回上一步填写信息');
        }

    }

    /**
     * 商家入驻第四步
     * @desc 商家入驻第四步
     * @return ret 200 成功
     */
    public function seller_step_four()
    {
        $user_id = $this->user_id;
        $where['user_id'] = $user_id;
        $data['shopNameSuffix'] = $this->shop_suffix;
        $data['rz_shopName'] = $this->shop_name;
        $data['hopeLoginName'] = $this->login_name;
        $nformation_where['user_id'] = $user_id;
        $shop_information = $this->medoo->get('merchants_shop_information', array('merchants_audit(status)', 'shop_id'), $nformation_where);
        if ($shop_information['status'] == 1) {
            $this->api_error_msg('您已经入驻了该平台');
        }
        $shop = $this->medoo->get('merchants_steps_fields', 'fid', $where);
        if (empty($shop) || empty($shop_information['shop_id'])) $this->api_error_msg('请返回上一步填写信息');
        $or_where['user_id[!]'] = $user_id;
        $or_where['OR'] = array('rz_shopName' => $data['rz_shopName'], 'hopeLoginName' => $data['hopeLoginName']);
        $or_res = $this->medoo->get('merchants_shop_information', 'shop_id', $or_where);
        if ($or_res) $this->api_error_msg('店铺名称已存在或登录名已存在');
        $res = $this->medoo->update('merchants_shop_information', $data, $where);
        if ($res) {
            $data1['steps_site'] = 4;
            $this->medoo->update('merchants_steps_fields', $data1, $where);
            $this->api_success_msg('操作成功');
        }
    }

    /**
     * 商家入驻第五步
     * @desc 商家入驻第五步
     * @return int status 0审核中1通过
     * @return string shop_name 店铺名称
     */
    public function seller_step_five()
    {
        $user_id = $this->user_id;
        $where['user_id'] = $user_id;
        $shop = $this->medoo->get('merchants_steps_fields', 'fid', $where);
        $shop_information = $this->medoo->get('merchants_shop_information', array('merchants_audit(status)', 'rz_shopName(shop_name)'), $where);
        $shop_information['login_name'] = $this->medoo->get('merchants_shop_information', 'hopeLoginName', array('user_id' => $user_id));
        if ($shop && $shop_information) {
            return array('info' => $shop_information);
        } else {
            return array('info' => array());
        }
    }

    /**
     * 添加收货地址
     * @desc 添加收货地址
     * @return int ret 200成功
     */
    public function add_address()
    {
        $data['user_id'] = $this->user_id;
        $data['consignee'] = $this->name;
        $data['country'] = 1;
        $data['province'] = $this->province;
        $data['city'] = $this->city;
        $data['district'] = $this->area;
        $data['address'] = $this->address;
        $data['mobile'] = $this->mobile;
        $data['is_default'] = $this->is_default;
        $data['tel'] = $data['mobile'];
        $count = $this->medoo->count('user_address', array('user_id' => $this->user_id));
        if ($count >= 5) {
            $this->api_error_msg('最多只能添加五个地址');
        }
        if ($count >= 1 && $data['is_default'] == 1) {
            $this->medoo->update('user_address', array('is_default' => 0), array('user_id' => $this->user_id));
        }
        if (!isMobile($data['mobile'])) $this->api_error_msg('请填写正确的手机号');
        $area_ids = $this->di->cache->get('area_ids');
        if (in_array($data['province'], $area_ids) && in_array($data['city'], $area_ids) && in_array($data['district'], $area_ids)) {
            $this->medoo->insert('user_address', $data);
            $insert_id = $this->medoo->id();
            if ($insert_id) {
                $this->api_success_msg('添加成功');
            } else {
                $this->api_error_msg('添加失败');
            }
        } else {
            $this->api_error_msg('请填写正确选择地区');
        }
    }

    /**
     * 编辑收货地址
     * @desc 编辑收货地址
     * @return int ret 200成功
     */
    public function edit_address()
    {
        $data['consignee'] = $this->name;
        $data['province'] = $this->province;
        $data['city'] = $this->city;
        $data['district'] = $this->area;
        $data['address'] = $this->address;
        $data['mobile'] = $this->mobile;
        $data['tel'] = $data['mobile'];
        $data['is_default'] = $this->is_default;
        $where['user_id'] = $this->user_id;
        $where['address_id'] = $this->address_id;
        if (!isMobile($data['mobile'])) $this->api_error_msg('请填写正确的手机号');
        $area_ids = $this->di->cache->get('area_ids');
        if (in_array($data['province'], $area_ids) && in_array($data['city'], $area_ids) && in_array($data['district'], $area_ids)) {
            if ($data['is_default'] == 1) {
                $this->medoo->update('user_address', array('is_default' => 0), array('user_id' => $this->user_id));
            }
            $res = $this->medoo->update('user_address', $data, $where);
            if ($res) {
                $this->api_success_msg('编辑成功');
            } else {
                $this->api_error_msg('编辑失败');
            }
        } else {
            $this->api_error_msg('请填写正确选择地区');
        }
    }

    /**
     * 删除收货地址
     * @desc 删除收货地址
     * @return int ret 200成功
     */
    public function del_address()
    {
        $where['user_id'] = $this->user_id;
        $where['address_id'] = $this->address_id;
        $res = $this->medoo->delete('user_address', $where);
        if ($res) {
            $this->api_success_msg('删除成功');
        } else {
            $this->api_error_msg('删除失败');
        }
    }

    /**
     * 用户地址列表
     * @desc 用户地址列表
     * @return string arae_address 地区拼接
     * @return string name 姓名
     * @return number mobile 手机号码
     * @return string address 详情地址
     * @return  int is_default 1默认0非默认
     * @return  int address_id 地址ID
     */
    public function my_address_list()
    {
        $where['user_id'] = $this->user_id;
        $where['ORDER'] = array('is_default' => 'DESC');
        $field = array('consignee(name)', 'province', 'city', 'district', 'mobile', 'address', 'address_id', 'is_default');
        $list = $this->medoo->select('user_address', $field, $where);
        if ($list) {
            foreach ($list as $key => $value) {
                $list[$key]['arae_address'] = $this->get_address($value['province'], $value['city'], $value['district']);
                unset($list[$key]['province']);
                unset($list[$key]['city']);
                unset($list[$key]['district']);
            }
            return array('list' => $list);
        }
        return array('list' => array());
    }

    /**
     * 用户地址详情
     * @desc 用户地址详情
     * @return string name 姓名
     * @return int province 省ID
     * @return int city 市ID
     * @return int district 地区ID
     * @return number mobile 手机号码
     * @return string address 详情地址
     * @return  int is_default 1默认0非默认
     */
    public function get_address_info()
    {
        $where['user_id'] = $this->user_id;
        $where['address_id'] = $this->address_id;
        $field = array('consignee(name)', 'province', 'city', 'district', 'address', 'mobile', 'is_default');
        $info = $this->medoo->get('user_address', $field, $where);
        $info['address_msg'] = $this->get_address($info['province'], $info['city'], $info['district']);
        return $info;
    }

    /**
     * 用户商品收藏列表
     * @desc 用户商品收藏列表
     * @return string goods_name 商品名称
     * @return float goods_price 商品价格
     * @return string goods_thumb 商品图片
     * @return int goods_id 商品ID
     * @return int rec_id 收藏ID
     */
    public function user_goods_collection()
    {
        $where['collect_goods.user_id'] = $this->user_id;
        $join['[>]goods'] = array('goods_id');
        $where['LIMIT'] = array($this->limitStart, $this->limitEnd);
        $where['ORDER'] = array('collect_goods.rec_id' => 'DESC');
        $field = array('collect_goods.goods_id', 'goods.goods_name', 'goods.shop_price(goods_price)', 'goods.goods_thumb', 'collect_goods.rec_id');
        $list = $this->medoo->select('collect_goods', $join, $field, $where);
        $count = 0;
        if ($list) {
            $count = $this->medoo->count('collect_goods', $join, 'collect_goods.rec_id', $where);
        } else {
            $list = array();
        }
        $data['list'] = $list;
        $data['hasmore'] = $this->HasMore($count);
        return $data;
    }

    /**
     * 取消收藏
     * @param rec_id string 收藏id
     * @desc 取消收藏
     * @return int ret 200成功
     */
    public function user_collection_del()
    {
        $rec_id = $this->rec_id;
        $sql = 'delete from dsc_collect_goods WHERE user_id = ' . $this->user_id . ' and rec_id in (' . $rec_id . ')';
        $res = \PhalApi\DI()->notorm->collect_goods->queryAll($sql);
        if ($res == null) {
            $this->api_success_msg('删除成功');
        } else {
            $this->api_error_msg('删除失败');
        }

    }

    /**
     * 用户店铺收藏列表
     * @desc 用户店铺收藏列表
     * @return string shop_name 店铺名称
     * @return int ru_id 店铺ID
     * @return string store_img 店铺图片
     * @return int collect_number 已收藏人数
     * @return int rec_id 收藏ID
     */
    public function user_store_collection()
    {
        $where['user_id'] = $this->user_id;
        $where['LIMIT'] = array($this->limitStart, $this->limitEnd);
        $where['ORDER'] = array('rec_id' => 'DESC');
        $field = array('rec_id', 'ru_id');
        $list = $this->medoo->select('collect_store', $field, $where);
        $count = 0;
        if ($list) {
            foreach ($list as $key => $value) {
                $info = $this->get_shop_name($value['ru_id']);
                $list[$key]['shop_name'] = $info['shop_name'];
                $list[$key]['store_img'] = $info['brand_thumb'];
                $list[$key]['collect_number'] = $info['collect_number'];
            }
            $count = $this->medoo->count('collect_store', 'rec_id', $where);
        } else {
            $list = array();
        }
        $data['hasmore'] = $this->HasMore($count);
        $data['list'] = $list;
        return $list;
    }

    /**
     * 用户上传头像
     * @desc 用户上传头像
     * @return string img_name 图片名称
     */
    public function user_upload_head()
    {
        $where['user_id'] = $this->user_id;
        $save = '/data/images_user/';
        $upload = ftp_uplaod($save);
        $res = $upload->upfile($this->file);
        if ($upload->getError() && !empty($res['file'])) {
            $this->api_error_msg($upload->getError());
        } else {
            $savename = $res['file'];
            $result = $this->medoo->update('users', array('user_picture' => $savename), $where);
            if ($result) {
                return array('img_name' => $savename);
            } else {
                return array('img_name' => '');
            }
        }

    }

    /**
     * 获取用户余额
     * @desc 获取用户余额
     * @return float
     */
    public function get_user_money()
    {
        $user_id = $this->user_id;
        return $this->medoo->get('users', 'user_money', array('user_id' => $user_id));
    }

    /**
     * 用户足迹
     * @desc 用户足迹
     * @return array 字段请看商品详情
     */
    public function user_foot_goods()
    {
        $goods = $this->di->cache->get($this->user_id . 'goods');
        if ($goods) {
            foreach ($goods as $key => $value) {
                $goods[$key]['time'] = get_time_shaft($value['time']);
            }
            return $goods;
        }
        return array();
    }

    /**
     * 删除用户足迹
     * @desc 删除用户足迹
     * @return ret 200 成功
     */
    public function user_foot_del()
    {
        $goods_id = explode(',', trim($this->goods_id, ','));
        $goods = $this->di->cache->get($this->user_id . 'goods');
        if ($goods) {
            foreach ($goods as $key => $value) {
                if (in_array($value['goods_id'], $goods_id)) {
                    unset($goods[$key]);
                }
            }
            $this->di->cache->delete($this->user_id . 'goods');
            $this->di->cache->set($this->user_id . 'goods', $goods, 30 * 86400);
            $this->api_success_msg('删除成功');
        }
        $this->api_error_msg('删除失败');
    }

    /**
     * 我的试用
     * @desc 我的试用
     * @return string goods_name 商品名称
     * @return string shop_price 商品价格
     * @return string goods_thumb 商品图片
     * @return string addtime 申请时间
     * @return string order_id 订单ID(为真时可以查看订单详情)
     * @return int act_id 活动ID
     * @return int status 0审核中1审核通过2未通过
     * @return int num 浏览量
     */
    public function user_try_goods()
    {
        $user_id = $this->user_id;
        $status = $this->status;
        $join['[>]goods'] = array('goods.goods_id' => 'try_apply.goods_id');
        $join['[>]try_apply_log'] = array('try_apply_log.apply_id' => 'try_apply.apply_id');
        $where['try_apply.user_id'] = $user_id;
        $where['try_apply.status[!]'] = -1;
        $where['LIMIT'] = array($this->limitStart, $this->limitEnd);
        $where['ORDER'] = array('try_apply.apply_id' => 'DESC');
        if ($status && in_array($status, array('0', '1', '2'))) {
            $where['try_apply.status'] = $status;
        }
        $field = array('goods.goods_name', 'goods.shop_price', 'goods.goods_thumb', 'try_apply.addtime', 'try_apply.status', 'try_apply.order_id', 'try_apply.act_id', '#COUNT(try_apply_log.log_id)(num)');
        $list = $this->medoo->select('try_apply', $join, $field, $where);
        $count = 0;
        if (isset($list[0]['goods_name']) && $list[0]['goods_name']) {
            foreach ($list as $key => $value) {
                $list[$key]['addtime'] = time_format($value['addtime']);
            }
            unset($where['LIMIT']);
            unset($where['ORDER']);
            $count = $this->medoo->count('try_apply', $join, 'try_apply.apply_id', $where);
        } else {
            $list = array();
        }
        $data['list'] = $list;
        $data['hasmore'] = $this->HasMore($count);
        return $data;
    }

    /**
     * 会员众筹订单列表
     * @desc 会员订单列表
     * @return string order_sn 订单号
     * @return array order_status 订单状态(status=1取消2确认收货3评论)(return=1申请售后)(is_del==1删除)(is_pay==1支付)
     * @return string add_time 订单生成时间
     * @return int order_id 订单id
     * @return float order_amount 订单总金额
     * @return string invoice_no 当为真是可以查看物流
     */
    public function user_crowd_orders()
    {
        $user_id = $this->user_id;
        $status = $this->status;
        $join['[>]order_goods'] = array('order_id');
        $join['[>]goods'] = array('goods.goods_id' => 'order_goods.goods_id');
        $join['[>]goods_activity'] = array('goods_activity.goods_id' => 'order_goods.goods_id');
        $where['order_info.extension_code'] = 'group_buy';
        $where['order_info.user_id'] = $user_id;
        $where['LIMIT'] = array($this->limitStart, $this->limitEnd);
        if ($status && in_array($status, array('0', '1', '2'))) {
            $where['goods_activity.is_finished'] = intval($status);
        }
        $field = array('order_info.order_id', 'goods.goods_name', 'goods.goods_thumb', 'order_goods.goods_number', 'goods_activity.is_finished', 'order_info.order_status', 'order_info.add_time', 'order_info.shipping_status', 'order_info.pay_status', 'order_info.order_amount', 'order_info.order_sn', 'order_info.pay_time', 'order_info.invoice_no');
        $list = $this->medoo->select('order_info', $join, $field, $where);
        $sign = $this->medoo->get('shop_config', 'value', array('code' => 'sign'));
        $count = 0;
        if (isset($list[0]['order_id'])) {
            foreach ($list as $key => $value) {
                $list[$key]['order_status'] = $this->get_order_status($value, $sign);
                $list[$key]['add_time'] = time_format($value['add_time']);
                unset($list[$key]['pay_time']);
                unset($list[$key]['pay_status']);
                unset($list[$key]['shipping_status']);
                unset($list[$key]['sign1']);
            }
            unset($where['LIMIT']);
            $count = $this->medoo->count('order_info', $join, 'order_info.order_id', $where);
        }
        $data['lsit'] = $list;
        $data['hasmore'] = $this->HasMore($count);
        return $data;
    }

    public function myEarnings()
    {

    }

    public function myTeam()
    {

    }

    /**
     * 我的优惠券
     * @return
     */
    public function myCoupons()
    {
        $user_id = $this->user_id;
        $sql = 'select u.is_use,u.order_id,c.cou_name,c.cou_man,c.cou_money,c.cou_goods,c.cou_start_time,c.cou_end_time,c.cou_ok_cat from dsc_coupons_user as u INNER JOIN dsc_coupons as c ON  u.cou_id = c.cou_id WHERE  u.user_id =' . $user_id;
        $coupons = \PhalApi\DI()->notorm->coupons_user->queryAll($sql, array());
        return $coupons;
    }
}
